package 剑指offer;

/**
 * @author zhangjunfei
 * @date 2023/8/7 10:48
 */
public class O_53_01 {
    public int search(int[] nums, int target) {

        int index = search2(nums, target);
        if (index < 0) {
            return 0;
        }
        int count = 1;
        int temp2 = index;
        int temp = index;
        while (temp2 >= 0) {

            if (nums[temp2] != target) {
                break;
            }
            if (index != temp2) {
                count++;
            }
            temp2--;

        }
        while (temp < nums.length) {

            if (nums[temp] != target) {
                break;
            }
            if (index != temp) {
                count++;
            }
            temp++;

        }
        return count;

    }

    public static int search2(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                low = mid+1;
            }else{
                high = mid-1;
            }

        }
        return -1;
    }
}
